﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Entities;
using System.Data;
using System.Data.SqlClient;

namespace Data
{
    public class CardData
    {
        Connect cn = new Connect();

        public void SetPIN()
        {
        }

        public string GetPIN(CardsEntity cE)
        {
            int Npara = 1;
            string[] name = new string[Npara];
            object[] value = new object[Npara];
            name[0] = "@CardNo";
            value[0] = cE.CardNo;
            string sql = "dbo.CardsSelectByID";
            DataTable gP = cn.GetData(sql, name, value, Npara);
            return gP.Rows[0]["PIN"].ToString();
        }

        public int GetAccountID(CardsEntity cE)
        {
            int Npara = 1;
            string[] name = new string[Npara];
            object[] value = new object[Npara];
            name[0] = "@CardNo";
            value[0] = cE.CardNo;
            string sql = "dbo.CardsSelectByID";
            DataTable gP = cn.GetData(sql, name, value, Npara);
            return int.Parse(gP.Rows[0]["AccountID"].ToString());
        }

        public void Block(CardsEntity cE)
        {
            CardsEntity cdE = CardsEntity(cE.CardNo);
            int Npara = 7;
            string[] name = new string[Npara];
            object[] value = new object[Npara];
            name[0] = "@CardNo"; value[0] = cdE.CardNo;
            name[1] = "@Status"; value[1] = "block";
            name[2] = "@AccountID"; value[2] = cdE.AccountID;
            name[3] = "@PIN"; value[3] = cdE.PIN;
            name[4] = "@StartDate"; value[4] = cdE.StartDate;
            name[5] = "@ExpiredDate"; value[5] = cdE.ExpiredDate;
            name[6] = "@Attempt"; value[6] = cdE.Attempt;
            string sql = "dbo.CardsUpDate";
            cn.NonQuery(sql, name, value, Npara);
        }

        public void UnBlock(CardsEntity cE)
        {
            CardsEntity cdE = CardsEntity(cE.CardNo);
            int Npara = 7;
            string[] name = new string[Npara];
            object[] value = new object[Npara];
            name[0] = "@CardNo"; value[0] = cdE.CardNo;
            name[1] = "@Status"; value[1] = "normal";
            name[2] = "@AccountID"; value[2] = cdE.AccountID;
            name[3] = "@PIN"; value[3] = cdE.PIN;
            name[4] = "@StartDate"; value[4] = cdE.StartDate;
            name[5] = "@ExpiredDate"; value[5] = cdE.ExpiredDate;
            name[6] = "@Attempt"; value[6] = 0;
            string sql = "dbo.CardsUpDate";
            cn.NonQuery(sql, name, value, Npara);
        }

        public int GetAttempt(CardsEntity cE)
        {
            int Npara = 1;
            string[] name = new string[Npara];
            object[] value = new object[Npara];
            name[0] = "@CardNo";
            value[0] = cE.CardNo;
            string sql = "dbo.CardsSelectByID";
            DataTable gP = cn.GetData(sql, name, value, Npara);
            return int.Parse(gP.Rows[0]["Attempt"].ToString());
        }

        public CardsEntity CardsEntity(string cNo)
        {
            int Npara = 1;
            string[] name = new string[Npara];
            object[] value = new object[Npara];
            name[0] = "@CardNo";
            value[0] = cNo;
            string sql = "dbo.CardsSelectByID";
            DataTable gP = cn.GetData(sql, name, value, Npara);

            CardsEntity cE = new CardsEntity
            {
                CardNo = gP.Rows[0][0].ToString(),
                Status = gP.Rows[0][1].ToString(),
                AccountID = (int)gP.Rows[0][2],
                PIN = gP.Rows[0][3].ToString(),
                StartDate = (DateTime)gP.Rows[0][4],
                ExpiredDate = (DateTime)gP.Rows[0][5],
                Attempt = (int)gP.Rows[0][6]
            };
            return cE;
        }
    }
}
